package com.yx.cloud.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.iot.base.service.Response;
import com.iot.base.service.req.ModifyUserInfoParam;
import com.iot.base.service.resp.LoginResponse;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.AmendnicknameActivtyBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LoadProgressDialog;
import com.yx.cloud.utils.RomUtils;
import com.iot.common.utils.Event;
import com.yx.cloud.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 修改昵称
 */
public class AmendNickNameActivty extends BaseActivity implements View.OnClickListener {

    LoadProgressDialog loadProgressDialog;
    private String mNickName;
    private AmendnicknameActivtyBinding binding;

    @Override
    public void onCreate() {
        binding = AmendnicknameActivtyBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {

        loadProgressDialog = new LoadProgressDialog(this);

        String nickName = getIntent().getStringExtra("nickName");
        binding.etInputNewNikename.setText(nickName);
//        if (binding.etInputNewNikename.getText().toString().trim() != "") {
//            binding.btnNikenameConfirm.setEnabled(true);
//        }else {
//            binding.btnNikenameConfirm.setEnabled(false);
//        }

        binding.etInputNewNikename.setSelection(binding.etInputNewNikename.getText().toString().trim().length());

        //监听是否获得焦点,清空内容
        binding.etInputNewNikename.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i("TAG", " :111111111111111111111111111111 " + hasFocus);
                if (hasFocus) {
                    binding.etInputNewNikename.setText("");
                }
            }
        });

        /**
         * 对帐号密码输入框进行监听
         */
        binding.etInputNewNikename.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString().trim())) {
                    binding.btnNikenameConfirm.setEnabled(false);
                } else {
                    binding.btnNikenameConfirm.setEnabled(true);
                    return;
                }
            }
        });

        binding.ivNikenameReturn.setOnClickListener(this);
        binding.btnNikenameConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_nikenameReturn:
                Intent intent = getIntent();
                Bundle bundle = intent.getExtras();
                bundle.putString("mNickName", "");
                intent.putExtras(bundle);
                setResult(5, intent);
                Intent_utils.backIntent(this);
                break;
            case R.id.btn_NikenameConfirm:
                mNickName = binding.etInputNewNikename.getText().toString().trim();
                if (mNickName.length() > 15) {
                    Toast.makeText(AmendNickNameActivty.this, getString(R.string.U_T_Text_Long), Toast.LENGTH_LONG).show();
                    return;
                }

                loadProgressDialog.show();
                resetNikename(mNickName);
                break;
        }
    }

    private void resetNikename(String nikeName) {
        ModifyUserInfoParam modifyUserInfoParam=ModifyUserInfoParam.newBuilder().nickname(nikeName).build();
        AppContext.getAccountService().modifyUserInfo(modifyUserInfoParam);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        loadProgressDialog.dismiss();
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_SET_NICKNAME:
                if (event.isResult()) {
                    AppContext.getAccountService().getUserInfo();//同步用户信息
                    Toast.makeText(AmendNickNameActivty.this, getString(R.string.MY_T_NickName_Edit_Ok), Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.putExtra("mNickName", mNickName);
                    setResult(5, intent);
                    finish();
                } else {
                    Response response= (Response) event.getContent();
                    if (response.getErrorCode()== LoginResponse.ERROR_NETWORK) {
                        ToastUtil.showShort(this,R.string.U_T_Network_Err);
                    }else if (response.getErrorCode()==LoginResponse.ERROR_SERVER) {
                        ToastUtil.showShort(this, getString(R.string.U_T_Server_Err));
                    } else {
                        ToastUtil.showShort(this, response.getData().toString());
                    }
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //禁止划屏关闭界面
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            bundle.putString("mNickName", "");
            intent.putExtras(bundle);
            setResult(5, intent);
            Intent_utils.backIntent(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
